package com.jy.appmarket.screenlock.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class ProgressView extends View {

	private int mBackground = 0x00000000;
	private int mForeground = 0xfff5f5f5;
	private int mProgress = 50;
	// private int mHeight = 0;
	// private int mWidth = 0;
	private Paint mBackgroundPaint;
	private Paint mForegroundPaint;
	private Paint mTextPaint;
	private boolean mShowProgress = false;
	private Path mProgressPath = new Path();
	private Rect mTargetRect = new Rect(50, 50, 1000, 200);

    public ProgressView(Context context) {
		super(context);
		init();
	}

	public ProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ProgressView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {

		mBackgroundPaint = new Paint();
		mBackgroundPaint.setStyle(Style.FILL);
		mBackgroundPaint.setColor(mBackground);

		mForegroundPaint = new Paint();
		mForegroundPaint.setStyle(Style.FILL);
		mForegroundPaint.setColor(mForeground);

		mTextPaint = new Paint();
		mTextPaint.setColor(Color.WHITE);
		mTextPaint.setTextSize(36);
		mTextPaint.setTextAlign(Paint.Align.CENTER);
	}

	public void setBackground(int color) {
		mBackground = color;
		init();
		invalidate();
	}

	public void setForeground(int color) {
		mForeground = color;
		init();
		invalidate();
	}

	public void setProgress(int progress) {
		mProgress = progress;
		invalidate();
	}

	public void setShowProgressText(boolean show) {
		mShowProgress = show;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawRect(0, 0, getWidth(), getHeight(), mBackgroundPaint);
//		canvas.drawRect(0, 0 , getWidth()*mProgress / 100, getHeight(),
//				mForegroundPaint);
		float mainProgress = getWidth() * mProgress / 100;
//		float secondProgress = getWidth() * (mProgress - 20) / 100;
		float secondProgress = getWidth() * mProgress / 100;
		if(mainProgress >= getWidth() - getPaddingRight()){
			mainProgress = getWidth() - getPaddingRight();
			secondProgress = mainProgress;
		}
		if(secondProgress < 0){
			secondProgress = 0;
		}
		mProgressPath.moveTo(0, 0);
		mProgressPath.lineTo(mainProgress, 0);
		mProgressPath.lineTo(secondProgress, getHeight());
		mProgressPath.lineTo(0, getHeight());
		mProgressPath.close();
        canvas.drawPath(mProgressPath, mForegroundPaint);
		if (mShowProgress) {
			FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
			int baseline = mTargetRect.top
					+ (mTargetRect.bottom - mTargetRect.top - fontMetrics.bottom + fontMetrics.top)
					/ 2 - fontMetrics.top;
			// canvas.drawText(mProgress + "%", 0, 0, mTextPaint);
			canvas.drawText(mProgress + "%", mTargetRect.centerX(), baseline, mTextPaint);
		}
	}

}
